//定义接口
let { list } = require('./Mock')

module.exports = (app) => {
  //获取数据列表接口
  app.get('/city', (req, res) => {
    res.send({
      list,
      code: 1
    })
  })

  //新增接口
  app.get('/add/city', (req, res) => {
    const { name, phone, region, address } = req.query
    list = [...list, { id: Math.random(), name, phone, region, address }]
    res.send({
      code: 200,
      msg: '添加成功'
    })
  })

  //编辑接口
  app.get('/edit/city', (req, res) => {
    const { name, phone, region, address, id } = req.query
    list = list.map(item => {
      if (item.id == id) {
        return { id: item.id, name, phone, region, address }
      }
      return item
    })
    res.send({
      code: 200,
      msg: '编辑成功'
    })
  })
  //删除接口
  app.get('/del/city', (req, res) => {
    //通过id删除
    const { id } = req.query
    // console.log(id, 'id')
    list = list.filter(item => item.id != id)
    res.send({
      code: 200,
      msg: '删除成功'
    })
  })
}